﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using WebSiteAdminTool.Web.Services.Base;
using WebSiteAdminTool.Web.Services.Contracts;
using System.Web.Security;
using WebSiteAdminTool.Web.Services.DTOs;
using System.Data;
using WebSiteAdminTool.Web.DataAccess.Interfaces;
using WebSiteAdminTool.Web.ConfigManager.Interfaces;
using WebSiteAdminTool.UI.Ioc;

namespace WebSiteAdminTool.Web.Services
{
    public class RolesService : BaseService, IRolesService
    {
        #region private data

        private IRolesDataAccess _rolesDataAccess = null;
        private IRolesConfigManager _rolesConfigManager = null;

        #endregion

        #region ctors

        public RolesService()
        {
            _rolesDataAccess = ServiceLocator.Instance.RolesDataAccess;
            _rolesConfigManager = ServiceLocator.Instance.RolesConfigManager;
        }

        #endregion

        public RoleData[] GetRoles()
        {
            CheckRolesEnabled();

            // load roles directly from db
            // instead of a default limited version Roles.GetAllRoles()
            var dbRoles = _rolesDataAccess.GetRoles();
            var roleUsages = _rolesDataAccess.GetRolesUsages();
            return dbRoles.Select(dbRole =>
                {
                    int usersInRole = roleUsages.ContainsKey(dbRole.RoleId) ? roleUsages[dbRole.RoleId] : 0;
                    return new RoleData(dbRole, usersInRole);
                }).ToArray();

            //RoleData[] roles = null;
            //roles = Roles.GetAllRoles().Select(r =>
            //{
            //    string loweredName = r.ToLower();

            //    int usersInRole = Roles.GetUsersInRole(r).Length;
            //    string comment = roleComments.ContainsKey(loweredName) ? roleComments[loweredName] : string.Empty;
            //    return new RoleData(r, usersInRole, comment);
            //}).ToArray();

            // return roles;
        }

        public RoleData CreateRole(RoleData role)
        {
            CheckRolesEnabled();
            if (null != role && role.Validate())
            {
                Roles.CreateRole(role.RoleName);
                // get role id
                Guid roleId = _rolesDataAccess.GetRoleId(role.RoleName);
                // update other role attributes
                _rolesDataAccess.UpdateRole(roleId, role.RoleName, role.Description);
            }
            return role;
        }

        public RoleData UpdateRole(RoleData role)
        {
            CheckRolesEnabled();
            if (null != role && role.Validate())
            {
                // update role attributes
                _rolesDataAccess.UpdateRole(role.RoleId, role.RoleName, role.Description);
            }

            return role;
        }

        public bool RemoveRole(Guid roleId)
        {
            CheckRolesEnabled();
            // Remove role from users
            _rolesDataAccess.RemoveRoleUsages(roleId);
            // default functionality -> Roles.DeleteRole(role.RoleName, false);
            return _rolesDataAccess.RemoveRole(roleId);
        }

        public bool RemoveRoleUsages(Guid roleId)
        {
            CheckRolesEnabled();
            // TODO: Remove role from users
            return _rolesDataAccess.RemoveRoleUsages(roleId);
        }

        #region helpers

        protected bool CheckRolesEnabled()
        {
            if (!IsRoleManagementEnabled)
                throw new InvalidOperationException("Roles are not enabled");

            return true;
        }

        #endregion
    }
}
